package com.yanqu.road.server.cmd.inn;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.inn.InnProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.inn.InnModule;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_INN_CLICK_MONEY, desc = "点击钱箱")
public class InnClickMoneyCmd extends AbstractCommand {

    private Property reward;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        if (!SystemOpenMgr.systemOpen(player, eSystemId.Inn.getValue())) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        long nowTime = System.currentTimeMillis() / 1000;

        InnModule module = player.getModule(InnModule.class);

        InnProto.InnDetailInfoMsg innInfo = module.getInnInfo();
        if (innInfo == null) {
            return;
        }
        long lastClickMoneyTime = innInfo.getLastClickMoneyTime();
        // 30秒 配置类
        if (nowTime - lastClickMoneyTime < GameConfig.INN_COOK_GET_MONEY_MIN_TIME) {//领取太频繁
            ret = GameErrorCode.E_INN_GET_MONEY_TIME_OFEN;
            return;
        }
        reward = module.getBoxReward();
    }

    @Override
    public void sendData(GamePlayer player) {
        InnProto.InnClickMoneyRspMsg.Builder respMsg = InnProto.InnClickMoneyRspMsg.newBuilder().setRet(ret);
        if (ret == 0) {
            InnProto.InnDetailInfoMsg innInfo = player.getModule(InnModule.class).getInnInfo();
            if (null != innInfo) {
                respMsg.setInnDetailInfo(innInfo);
            }
            if (null != reward) {
                respMsg.setReward(PropertyHelper.parsePropertyToString(reward));
            } else {
                respMsg.setRet(GameErrorCode.E_INN_BOX_NULL); //钱箱没有钱
            }
        }
        player.sendPacket(Protocol.U_INN_CLICK_MONEY, respMsg);

    }
}
